// cd /projects/hsieh_project/proj_201809/code_2_202011/
// qstata mkt_d_ind_vs_top_mkt.do &

set linesize 255
capture log close
log using /projects/hsieh_project/proj_201809/code_2_202011/mkt_d_ind_vs_top_mkt_log, replace text

clear all
cd /projects/

di "$S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_do "${dir_proj}/code_2_202011/"
global dir_data "${dir_proj}/data/"
global dir_out "${dir_proj}/output/202011_main/"
noi capture mkdir ${dir_out}
global dir_outf "${dir_out}/mkt_d_ind_vs_top_mkt/"
noi capture mkdir ${dir_outf}

global year1 = 1977
global year2 = 2013

global gl_perc = "10"
local l_perc = "10"

do "/projects/hsieh_project/code_0_general/f_rounding.do"
do "/projects/hsieh_project/code_0_general/f_reg.do"

global ds_ind "${dir_data}/ind_sum_all"

global lvmkt = "est msa1983 msa1983cz zipcode fips msa czone"

//==============================================================================
// Combine two sets of geo variables

do ${dir_do}/mkt_d_ind_0.do "geo"

save ${dir_outf}/mkt_d_ind_top_vs_all_mkt, replace

//==============================================================================
// Plot the trend

local vmkt = "fips"
local iperc = 1
local y = "ln_mu_mkt_`vmkt'_d_`iperc'" 
local x = "ln_mu_mkt_`vmkt'_d"
local y_lab = "{&Delta} ln(Average Market FIPS of Top `iperc'% Firms)"
local x_lab = "{&Delta} ln(Average Market FIPS of All Firms)"

capture program drop f_fig
program f_fig

args vmkt iperc iperc_w y x y_lab x_lab p_fig

// Find max and min range for figure:
local y_min = 99999
local y_max = -99999

foreach ivar of var `x'_* {
	sum `ivar'
	local y_min = min(`y_min', `r(min)')
	local y_max = max(`y_max', `r(max)')
}
sum `x'
local x_min = `r(min)'
local x_max = `r(max)'

reg `y' `x' [aw=w_sv_`iperc_w']
f_reg_append "fr_reg" "`x'"

local b_x: di %4.2f _b[`x'] // round(_b[`x'],0.001)
local reg_r2: di %4.2f `e(r2)' // round(_b[`x'],0.001)
local reg_N = `e(N)'
di `b_x' `b_N'
if "`p_fig'" != "0" {
twoway (scatter `y' `x', msize(vsmall)) ///
	(lfit `y' `x' [aw=w_sv_`iperc_w']), ///
	ytitle("`y_lab'") xtitle("`x_lab'") ///
	legend(off) ///
	note("reg y x [aw=w_sv_`iperc_w']: _b[x] = `b_x', R2 = `reg_r2'; Top firms are defined based on markets.")
	
	// ysc(range(`y_min' `y_max')) xsc(range(`x_min' `x_max')) ///
}
end

f_reg_create "fr_reg" "1"

//==============================================================================
// Top 10% and 1%

foreach vmkt in $lvmkt {
	
	foreach iperc in 10 1 {

		local y = "ln_mu_mkt_`vmkt'_d_`iperc'" 
		local x = "ln_mu_mkt_`vmkt'_d"
		local y_lab = `"{&Delta} ln(Average # of `=upper("`vmkt'")' of Top `iperc'% Firms)"'
		local x_lab = `"{&Delta} ln(Average # of `=upper("`vmkt'")' of All Firms)"'
		
		f_fig "`vmkt'" "`iperc'" "`iperc'" "`y'" "`x'" "`y_lab'" "`x_lab'"
		
		graph export ${dir_outf}/Y_ln_mkt_top_d_X_ln_mkt_ind_d_SPEC_`vmkt'_`iperc'.png, replace width(3000) height(2000)
		
        /*
		local y = "mu_mkt_`vmkt'_d_`iperc'" 
		local x = "mu_mkt_`vmkt'_d"
		local y_lab = `"{&Delta} Average # of `=upper("`vmkt'")' of Top `iperc'% Firms"'
		local x_lab = `"{&Delta} Average # of `=upper("`vmkt'")' of All Firms"'
		
		f_fig "`vmkt'" "`i_perc'" "`y'" "`x'" "`y_lab'" "`x_lab'"
		
		graph export ${dir_outf}/Y_mkt_top_d_X_mkt_ind_d_SPEC_`vmkt'_`iperc'.png, replace width(3000) height(2000)
        */
	}

}

//==============================================================================
// Bottom 90%

foreach vmkt in $lvmkt {
	
	foreach iperc in 10b {
        
        local iperc = "10b"
		local y = "ln_mu_mkt_`vmkt'_d_`iperc'" 
		local x = "ln_mu_mkt_`vmkt'_d"
		local y_lab = `"{&Delta} ln(Average # of `=upper("`vmkt'")' of Bottom 90% Firms)"'
		local x_lab = `"{&Delta} ln(Average # of `=upper("`vmkt'")' of All Firms)"'
		
		f_fig "`vmkt'" "`iperc'" "10" "`y'" "`x'" "`y_lab'" "`x_lab'"
		
		graph export ${dir_outf}/Y_ln_mkt_top_d_X_ln_mkt_ind_d_SPEC_`vmkt'_`iperc'.png, replace width(3000) height(2000)
	}

}

f_reg_save "fr_reg" "${dir_outf}/reg_mkt_d_ind_vs_top"

//==============================================================================
// Deciles

f_reg_create "fr_reg" "1"

foreach vmkt in $lvmkt {
	
	foreach iperc in 10 20 30 40 50 60 70 80 90 100 {
        
		local y = "ln_mu_mkt_`vmkt'_d_`iperc'd" 
		local x = "ln_mu_mkt_`vmkt'_d"
		local y_lab = `"{&Delta} ln(Average # of `=upper("`vmkt'")' of `=`iperc'-10'-`iperc'% Firms)"'
		local x_lab = `"{&Delta} ln(Average # of `=upper("`vmkt'")' of All Firms)"'
		
		f_fig "`vmkt'" "`iperc'" "10" "`y'" "`x'" "`y_lab'" "`x_lab'" "0"
	}

}

f_reg_save "fr_reg" "${dir_outf}/reg_mkt_d_ind_vs_top_deciles"


di "Ended at $S_DATE $S_TIME"
capture log close
// End of do file
